<?php

namespace App;

use App\Models\UserMoneyLog;
use http\Exception\RuntimeException;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Hash;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    CONST TYPE = [
        '0' => '--请选择--',
        '1' => '充值',
        '2' => '扣款',
    ];
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    CONST PROXY = [
        1,
        2
    ];

    CONST PROXY_DESC = [
        //0, //普通用户
        1 => '代理', //代理
        2 => '经销商' //经销商
    ];

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }

    public static function getMobileById($user_id)
    {
        $user = self::find($user_id);
        if($user){
            return $user['mobile'];
        }
        return '';
    }

    public static function getOneById($user_id, $field = null){
        $user = self::find($user_id);
        if($user){
            if(in_array($field, $user->toArray())){
                return $user[$field];
            }
            return $user->toArray();
        }
        return $user;
    }

    public static function getUserByOpenId($openid,$toArray = true)
    {
        $user = self::where('openid', $openid)->first();
        return $user;
    }

    public static function getUserByName($name)
    {
        $user = self::where('name', $name)->first();
        return $user;
    }

    //代理登录
    public static function proxyLogin($input)
    {
        $user = self::getUserByName($input['name']);
        if (empty($user)) {
            uexception('代理不存在');
        }
        if (!Hash::check($input['password'],$user['password'] )) {
            uexception('密码错误');
        }
        $user->is_proxy = 1;
        $user->save();
        return $user;
    }

    public static function isProxy()
    {
        $user = auth()->user();
        if(isset($user['is_proxy']) && $user['is_proxy'] == 1){
            return 1;
        }
        return 0;
    }

    public static function moneyChange(array $params,User $user)
    {
        if ($params['type'] == 1) {
            $user->increment('money', $params['change']);
        }
        if ($params['type'] == 2) {
            if ($params['change'] > $user['money']) {
                uexception('余额不足');
            }
            $user->decrement('money', $params['change']);
        }
        return UserMoneyLog::createLog($user, $params);
    }

    //管理员充值 或扣除资金
    public static function adminOptMoney($type, $change, User $user)
    {
        if (empty($type)) {
            return false;
        }
        $params = [
            'change' => $change,
            'type' => $type,
            'desc' => '管理员'.self::TYPE[$type],
        ];

        return User::moneyChange($params, $user);
    }

    public static function total()
    {
        return self::count();
    }

    public static function getPorxyCount()
    {
        $where = [
            ['name', '!=', ''],
            ['password', '!=', ''],
        ];
        return self::where($where)->count();
    }
}
